package com.zhihaoliang.unscramble.ui.game

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

/**
 * @author 支豪亮
 * @date 2022/11/4 16:29
 * @des ViewModel containing the app data and methods to process the data
 */
class GameViewModel : ViewModel() {


    private var _score = MutableLiveData(0)

    /**
     * Variables are defined for the current count score
     */
    val scope: LiveData<Int>
        get() = _score


    /**
     *  List of words used in the game
     */
    private var _wordsList: MutableList<String> = mutableListOf()

    private lateinit var _currentWord: String

    private var _currentWordCount: MutableLiveData<Int> = MutableLiveData(0)

    /**
     * Variables are defined for the current count
     */
     val currentWordCount: LiveData<Int>
        get() = _currentWordCount


    private var _currentScrambledWord = MutableLiveData<String>()

    /**
     * Variables are defined for the current scrambled word
     */
    val currentScrambledWord: LiveData<String>
        get() = _currentScrambledWord

    init {
        getNextWord()
    }

    /*
    * Returns true if the current word count is less than MAX_NO_OF_WORDS.
    * Updates the next word.
    */
    fun nextWord(): Boolean {
        return if (_currentWordCount.value!! < MAX_NO_OF_WORDS) {
            getNextWord()
            true
        } else false
    }

    /*
    * Returns true if the player word is correct.
    * Increases the score accordingly.
    */
    fun isUserWordCorrect(playerWord: String): Boolean {
        if (playerWord.equals(_currentWord, true)) {
            increaseScore()
            return true
        }
        return false
    }

    /*
    * Re-initializes the game data to restart the game.
    */
    fun reinitializeData() {
        _score.value = 0
        _currentWordCount.value = 0
        _wordsList.clear()
        getNextWord()
    }


    /**
     * update currentWord and currentScrambledWord with the next word
     */
    private fun getNextWord() {
        val tempCurrentWord = allWordsList.random()

        //if the word is used before,get a new word
        if (_wordsList.contains(tempCurrentWord)) {
            getNextWord()
            return
        }

        _currentScrambledWord.value = scrambledWord(tempCurrentWord)
        _currentWord = tempCurrentWord
        Log.e("GameViewModel",
              "_currentWord : $tempCurrentWord   _currentScrambledWord : $_currentScrambledWord")
        _currentWordCount.value = _currentWordCount.value!! + 1
        _wordsList.add(tempCurrentWord)
    }

    /**
     * scrambled the Word
     * @param word word to scrambled
     */
    private fun scrambledWord(word: String): String {
        val tempCharArray = word.toCharArray()
        tempCharArray.shuffle()
        while (String(tempCharArray).equals(word, false)) {
            tempCharArray.shuffle()
        }
        return String(tempCharArray)
    }

    /*
   * Increases the game score if the player’s word is correct.
   */
    private fun increaseScore() {
        _score.value = _score.value!! + SCORE_INCREASE
    }
}